package com.gzw.scm.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gzw.scm.MyApplication;
import com.gzw.scm.R;
import com.gzw.scm.UpdateManager;
import com.gzw.scm.adapter.MainGVAdapter;
import com.gzw.scm.base.BaseActivity;
import com.gzw.scm.bean.Constant;
import com.gzw.scm.utils.AppUtils;
import com.gzw.scm.utils.JsonUtil;
import com.gzw.scm.utils.T;
import com.gzw.scm.utils.TwitterRestClient;
import com.gzw.scm.view.XCSlideMenu;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2017/2/13.
 * 主页
 */

public class MainActivity extends BaseActivity {
    private Context mContext;
    private Toolbar toolbar;
    private Intent intent;
    private long exitTime = 0;
    private String NowVersion;
    private XCSlideMenu xcSlideMenu;
    private TextView tv_zh,tv_xm,tv_bbh;
    private RelativeLayout rl_one,rl_two;
    private GridView gv_list;
    private List<String> ls_name;
    private Button btn_glzxzb,btn_ck;
    private Logger log;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        xcSlideMenu = (XCSlideMenu) findViewById(R.id.slideMenu);
        tv_zh = (TextView) findViewById(R.id.ic_zh).findViewById(R.id.nav_userName);
        tv_xm = (TextView) findViewById(R.id.ic_zh).findViewById(R.id.nav_accunt);
        tv_bbh = (TextView) findViewById(R.id.ic_zh).findViewById(R.id.nav_bbh);
        rl_one = (RelativeLayout)findViewById(R.id.rl_one);
        rl_two = (RelativeLayout)findViewById(R.id.rl_two);
        gv_list=(GridView)findViewById(R.id.gv);
        btn_glzxzb = (Button)findViewById(R.id.btn_glzxzb);
        btn_ck = (Button)findViewById(R.id.btn_ck);

        //初始化 log
        log= Logger.getLogger(this.getClass());
    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("首页");
        setSupportActionBar(toolbar);
        tv_zh.setText("账户:" +"    "+ MyApplication.getUserInfo().getUsername().toString().trim());
        tv_xm.setText("姓名:" +"    "+ MyApplication.getUserInfo().getName().toString().trim());
        NowVersion = AppUtils.getVersionName(mContext);
        tv_bbh.setText("版本号:" +"    "+ NowVersion);

        intent=getIntent();
        int DepartmentType = intent.getIntExtra("DepartmentType",100);
        ls_name = new ArrayList<>();
        if(DepartmentType==0){
            btn_glzxzb.setBackgroundResource(R.mipmap.glzxzb_s);
            ls_name.clear();
            ls_name.add("zb-fp");
        }else if(DepartmentType==1){
            btn_ck.setBackgroundResource(R.mipmap.ck_s);
            ls_name.clear();
            ls_name.add("rdc-zxjs");
            ls_name.add("rdc-nxjs");
            ls_name.add("rdc-bd");
            ls_name.add("rdc-jb");

            ls_name.add("fb-dbjs");
            ls_name.add("fb-dbfc");

            ls_name.add("rdc-yjck");
            ls_name.add("rdc-ck");

            ls_name.add("rdc-pd");
            ls_name.add("fb-wx");

        }

        MainGVAdapter mainGVAdapter = new MainGVAdapter(mContext,ls_name);
        gv_list.setAdapter(mainGVAdapter);
    }

    @Override
    protected void initEvents() {
        rl_one.setOnClickListener(new myOnClick());
        rl_two.setOnClickListener(new myOnClick());
        toolbar.setNavigationOnClickListener(new myOnClick());
        gv_list.setOnItemClickListener((parent, view, position, id) ->  {

                switch (ls_name.get(position)){
                    case "zb-fp":
                        Intent mIntetn = new Intent();
                        mIntetn.setClass(mContext, SelectKhActivity.class);
                        mIntetn.putExtra("type","0");
                        startActivityForResult(mIntetn, 1);
                        break;

                    case "fb-dbfc":
                        Intent mIntetn2 = new Intent();
                        mIntetn2.setClass(mContext, SelectWBXXActivity.class);
                        mIntetn2.putExtra("type","1");
                        startActivityForResult(mIntetn2, 1);
                        break;
                    case "fb-dbjs":
                        Intent mIntetn3 = new Intent();
                        mIntetn3.setClass(mContext, SelectWBXXActivity.class);
                        mIntetn3.putExtra("type","0");
                        startActivityForResult(mIntetn3, 1);
                        break;

                    case "fb-wx":
                        Intent mIntetn5 = new Intent();
                        mIntetn5.setClass(mContext, WXZZXActivity.class);
                        startActivityForResult(mIntetn5, 1);
                        break;

                    case "rdc-zxjs":
                        Intent mIntetn7 = new Intent();
                        mIntetn7.setClass(mContext, JSZZXActivity.class);
                        saveLy("1");
                        startActivityForResult(mIntetn7, 1);
                        break;
                    case "rdc-nxjs":
                        Intent mIntetn8 = new Intent();
                        mIntetn8.setClass(mContext, JSZZXActivity.class);
                        saveLy("2");
                        startActivityForResult(mIntetn8, 1);
                        break;
                    case "rdc-ck":
                        Intent mIntetn9 = new Intent();
//                        mIntetn9.setClass(mContext, CKActivity.class);
                        mIntetn9.setClass(mContext, NewCkActivity.class);
                        startActivityForResult(mIntetn9, 1);
                        break;
                    case "rdc-yjck":
                        Intent mIntetn15 = new Intent();
                        mIntetn15.setClass(mContext, YJCKActivity.class);
                        startActivityForResult(mIntetn15, 1);
                        break;
                    case "rdc-bd":
                        Intent mIntetn11 = new Intent();
//                        mIntetn11.setClass(mContext, BDZZXActivity.class);
                        mIntetn11.setClass(mContext, NewBDZZXActivity.class);
                        mIntetn11.putExtra("type","0");
                        startActivityForResult(mIntetn11, 1);
                        break;
                    case "rdc-pd":
                        Intent mIntetn13 = new Intent();
                        mIntetn13.setClass(mContext, PDZZXActivity.class);
                        startActivityForResult(mIntetn13, 1);
                        break;
                    case "rdc-jb":
                        Intent mIntetn14 = new Intent();
                        mIntetn14.setClass(mContext, JCBDActivity.class);
                        startActivityForResult(mIntetn14, 1);
                        break;
                }

        });
        getVersion();
    }

    /**
     * Created by Wangzhiqiang on 2015-10-11
     * 单击事件
     */
    class myOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            switch (v.getId()) {
                case R.id.rl_one:
                    SharedPreferences sharedPreferences = getSharedPreferences(Constant.USERINFO_KEY,
                            Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putBoolean(Constant.IS_USER_LOGIN_KEY, false);
                    edit.commit();
                    Intent intent = new Intent();
                    intent.setClass(mContext, LoginActivity.class);
                    startActivity(intent);
                    finish();
                case R.id.rl_two:
                    SharedPreferences sharedPreferences1 = getSharedPreferences(Constant.USERINFO_KEY,
                            Context.MODE_PRIVATE);
                    SharedPreferences.Editor edit1 = sharedPreferences1.edit();
                    edit1.putBoolean(Constant.IS_USER_LOGIN_KEY, false);
                    edit1.commit();
                    finish();
                    break;
                default:
                    xcSlideMenu.switchMenu();
                    break;
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(mContext, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onNetChange(int netMobile) {
        // TODO Auto-generated method stub
        //在这个判断，根据需要做处理
        if(netMobile==-1){
            T.showShort(mContext,"当前网络不可用，请检查网络设置");
        }
    }

    private void saveLy(String ly) {
        SharedPreferences sharedPreferences = getSharedPreferences(Constant.USERINFO_KEY,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("ly",ly);
        edit.commit();
    }

    private void getVersion(){
        String version = AppUtils.getVersionName(mContext);
        RequestParams requestParams = new RequestParams();
        requestParams.add("method", "getVersion");
        requestParams.put("ver",version);
        TwitterRestClient.post(null, requestParams, new TextHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, String response) {
                try {
                    if (response == null || response.length() == 0) {
                        showAlertDialog("查询版本信息连接异常");
                        return;
                    }

                    String zt = JsonUtil.getMsg(response, "jiaoyan").toString();
                    if(zt.equals("false")){
                        UpdateManager updateManager = new UpdateManager(mContext);
                        updateManager.checkUpdateInfo();
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                    log.info("getVersion:"+e.toString()+"/respone="+response);
                    showAlertDialog(e.toString());
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String errorResponse, Throwable e) {
                showAlertDialog("查询版本信息异常");
            }

        });
    }


}
